<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\analysis\report\qq;

use app\enum\AnalysisEnum;
use app\enum\QqEnum;
use app\model\mongo\MongoQqAccountDayReportModel;
use app\service\analysis\QqTrait;
use app\service\analysis\report\ReportInterface;
use app\service\analysis\report\ReportService;

class AccountReportService extends ReportService implements ReportInterface
{
    use QqTrait;

    /**
     * @var int 同步类型
     */
    protected static int $taskType = AnalysisEnum::TASK_TYPE_ACCOUNT;

    /**
     * 日汇总查询.
     * @param array $where 查询语句
     */
    public function getDayStat(array $where = []): array
    {
        $model = new MongoQqAccountDayReportModel();
        $data = $model->cmd([
            'aggregate' => $model->getTable(),
            'pipeline' => [
                [
                    '$match' => $where,
                ],
                [
                    '$group' => [
                        '_id' => '$fund_type',
                        'totalPaid' => ['$sum' => '$paid'],
                        'totalBalance' => ['$sum' => '$balance'],
                    ],
                ],
                [
                    '$project' => [
                        '_id' => 1,
                        'totalPaid' => 1,
                        'totalBalance' => 1,
                    ],
                ],
            ],
            'cursor' => new \stdClass(),
            'explain' => false,
        ]);
        return [
            'cost' => $this->getTotalCost($data),
            'balance' => $this->getTotalBalance($data),
            'cash_cost' => $this->getTotalCashCost($data),
            'reward_cost' => $this->getTotalRewardCost($data),
        ];
    }

    /**
     * 获取账户赠款消耗.
     */
    public function getTotalRewardCost(array $data = []): float
    {
        return 0.000;
    }

    /**
     * 获取账面消耗(含赠款消耗).
     *
     * @param array $data 数据
     */
    private function getTotalCost(array $data = []): float
    {
        $paid = array_sum(array_column($data, 'totalPaid'));
        return floatval(bcdiv(strval($paid), '100', 3));
    }

    /**
     * 获取账户余额(含赠款消耗).
     *
     * @param array $data 数据
     */
    private function getTotalBalance(array $data = []): float
    {
        $balance = array_sum(array_column($data, 'totalBalance'));
        return floatval(bcdiv(strval($balance), '100', 3));
    }

    /**
     * 获取账户现金消耗.
     *
     * @param array $data 数据
     */
    private function getTotalCashCost(array $data = []): float
    {
        $arr = [];
        foreach ($data as $v) {
            if (! in_array($v['_id'], [QqEnum::FUND_TYPE_VIRTUAL, QqEnum::FUND_TYPE_SPECIAL_GIFT])) {
                $arr[] = $v['totalPaid'];
            }
        }
        $paid = array_sum(array_column($arr, 'totalPaid'));
        return floatval(bcdiv(strval($paid), '100', 3));
    }
}
